package com.xjgc.eip.twice.zxjeip2cccjc.controller;


import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjgc.common.constant.Consants;
import com.xjgc.common.entity.ReturnResult;
import com.xjgc.common.enums.ReturnStatus;
import com.xjgc.eip.twice.zxjeip2cccjc.entity.ZxjEip2cCcjc;
import com.xjgc.eip.twice.zxjeip2cccjc.service.ZxjEip2cCcjcService;
import com.xjgc.eip.twice.zxjeip2czzjc.service.ZxjEip2cZzjcService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 *  出厂检测 前端控制器
 * </p>
 * @author wxy
 * @since 2019-11-11
 */
@Api("出厂检测信息抓取接口")
@RestController
@RequestMapping("/apis/zxjEip2cCcjc")
public class ZxjEip2cCcjcController {

    @Autowired
    ZxjEip2cCcjcService zxjEip2cCcjcService;
    @Autowired
    ZxjEip2cZzjcService zxjEip2cZzjcService;
    /**
     * 出厂检测信息抓取接口
     */
    @ApiOperation("出厂检测信息抓取接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "amount", value = "查询条数",
                              required = true,dataType = "int")
    })
    @GetMapping(value = "/getData")
    @ResponseBody
    public ReturnResult getData(Integer amount,@RequestHeader(value = "supplierCode",required = false)String supplierCode){
        ReturnResult returnResult  = new ReturnResult();
        QueryWrapper<ZxjEip2cCcjc> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("\"IOFLAG\"", Consants.Unread)
                .last("limit " + (amount == null ? 100 : amount));
        if(StringUtils.isNotBlank(supplierCode)){
            queryWrapper.eq("\"SUPPLIER_CODE\"",supplierCode);
        }
        try {
            List<ZxjEip2cCcjc> result=zxjEip2cCcjcService.list(queryWrapper);
            //更新查询的数据为已读
            if(result!=null&&result.size()>0){
                for (ZxjEip2cCcjc zed:result) {
                    zed.setIoflag(Consants.read);
                    zed.setUpdateTime(new Date());
                }
                if (zxjEip2cCcjcService.updateBatchById(result)){
                    returnResult.setStatus(Integer.valueOf(ReturnStatus.OK.getCode()));
                    returnResult.setData(result);
                }else {
                    returnResult.setStatus(Integer.valueOf(ReturnStatus.FAIL.getCode()));
                    returnResult.setMessage("请求异常");
                }
            }
        }catch (Exception e){
            returnResult.setStatus(Integer.valueOf(ReturnStatus.FAIL.getCode()));
            returnResult.setMessage("请求异常");
        }
        return returnResult;
    }
    /**
     * 分页列表查询
     * 查询待审批的检测信息
     * @param zxjEip2cCcjc
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/list")
    public Result<?> queryPageList(ZxjEip2cCcjc zxjEip2cCcjc,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        zxjEip2cCcjc.setReviewStatus(Consants.REVIEW_STATUS1);//装置检测审批通过
        Page<ZxjEip2cCcjc> page = new Page<ZxjEip2cCcjc>(pageNo, pageSize);
        IPage<ZxjEip2cCcjc> pageList = zxjEip2cCcjcService.selectReviewCcjcListPage(page, zxjEip2cCcjc);
        return Result.ok(pageList);
    }
    /**
     * 已审核出厂检测信息分页列表查询
     * 查询已审核的检测信息
     * @param zxjEip2cCcjc
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/overlist")
    public Result<?> overlist(ZxjEip2cCcjc zxjEip2cCcjc,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<ZxjEip2cCcjc> queryWrapper = new QueryWrapper<ZxjEip2cCcjc>(zxjEip2cCcjc);
        Page<ZxjEip2cCcjc> page = new Page<ZxjEip2cCcjc>(pageNo, pageSize);
        IPage<ZxjEip2cCcjc> pageList = zxjEip2cCcjcService.page(page, queryWrapper);
        return Result.ok(pageList);
    }
    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        ZxjEip2cCcjc zxjEip2cCcjc = zxjEip2cCcjcService.getById(id);
        if(zxjEip2cCcjc==null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(zxjEip2cCcjc);
    }
    /**
     *   审核出厂检测信息
     *
     * @param zxjEip2cCcjc
     * @return
     */
    @PostMapping(value = "/review")
    public Result<?> add(@RequestBody List<ZxjEip2cCcjc> zxjEip2cCcjc) {
        try {
            boolean flag=zxjEip2cCcjcService.reviewCcjc(zxjEip2cCcjc);
            if (flag){
                return Result.ok("审核成功！");
            }else {
                return Result.error("审核失败！");
            }

        }catch (Exception e){
            return Result.error("审核失败！");
        }
    }
}

